//
//  Block.cpp
//  AvoidBlock
//
//  Created by 함 우식 on 12. 12. 10..
//
//

#include "Block.h"

#define BLOCK_SIZE 30

using namespace cocos2d;


Block::Block() 
{
    init();
}

bool Block::init()
{
	_isAlive = true;
    _currVelocity = ccp(0.0f, 0.0f);
    
	return true;
}

Block* Block::blockWithType(const int type)
{
    return initWithType(type);
}

Block* Block::initWithType(const int type)
{
    Block* block = new Block();
    block->_type = type;
    
    CCString* pStr = CCString::stringWithFormat("block_%d.png", type);
    block->_pBody = CCSprite::spriteWithFile(pStr->getCString());
    block->_pBody->setPosition(ccp(0.0f, 0.0f));
    block->_pBody->setVisible(true);
    block->addChild(block->_pBody);
    
    block->_pCollisionBox = CCSprite::spriteWithFile("block_bound.png");
    block->_pBody->setPosition(ccp(0.0f, 0.0f));
    block->_pCollisionBox->setVisible(false);
    block->addChild(block->_pCollisionBox);
    
    block->autorelease();
    return block;
}

void Block::update(float dt)
{
    _currVelocity = ccpAdd(_currVelocity, ccpMult(ccp(0.0f, -0.5f*BLOCK_SIZE), dt));
    _pBody->setPosition(ccpAdd(_pBody->getPosition(), ccpMult(_currVelocity, dt)));
    _pCollisionBox->setPosition(ccpAdd(_pCollisionBox->getPosition(), ccpMult(_currVelocity, dt)));
}

void Block::revive()
{
    _isAlive = true;
    _pBody->setVisible(false);
}

void Block::dead()
{
    _isAlive = false;
    _pBody->setVisible(false);
    _pBody->setPosition(ccp(0.0f, 0.0f));
}

void Block::setPosition(CCPoint position)
{
    _pBody->setPosition(position);
    _pCollisionBox->setPosition((position));
}

CCPoint Block::position()
{
    return _pBody->getPosition();
}

void Block::setVisible(bool visible)
{
    _pBody->setVisible(visible);
}

bool Block::isVisible()
{
    return _pBody->isVisible();
}

CCRect Block::boundingBox()
{
    return _pCollisionBox->boundingBox();
}